package com.gc.ykqx.ui;

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.RadioGroup;

import com.gc.ykqx.R;
import com.gc.ykqx.ui.adapter.LiveFragmentPagerAdapter;
import com.gc.ykqx.ui.fragment.ForeFragment;
import com.gc.ykqx.ui.fragment.HisFragment;
import com.gc.ykqx.ui.fragment.HomeFragment;
import com.gc.ykqx.ui.fragment.LiveFragment;
import com.gc.ykqx.ui.fragment.MonFragment;
import com.gc.ykqx.ui.custom.BaseActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private static final String TAG = "MainActivity";
    @BindView(R.id.main_tabs)
    RadioGroup rgTabs;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private Unbinder unbinder;
    private List<Fragment> fragments;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);
        initFragments();
        LiveFragmentPagerAdapter adapter = new LiveFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(5);
        rgTabs.setOnCheckedChangeListener(this);
    }

    private void initFragments() {
        fragments = new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        LiveFragment liveFragment = new LiveFragment();
        ForeFragment foreFragment = new ForeFragment();
        HisFragment hisFragment = new HisFragment();
        MonFragment monFragment = new MonFragment();
        fragments.add(homeFragment);
        fragments.add(liveFragment);
        fragments.add(foreFragment);
        fragments.add(hisFragment);
        fragments.add(monFragment);
    }

    /*
    底部导航按钮监听控制fragment显示隐藏
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rb_live:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rb_forecast:
                viewPager.setCurrentItem(2);
                break;
            case R.id.rb_datum:
                viewPager.setCurrentItem(3);
                break;
            case R.id.rb_radar:
                viewPager.setCurrentItem(4);
                break;
        }
    }

    /*
     页面销毁,释放资源
    */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        deleteDir(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ykqx/pic"));
    }
    /*
   删除图片
    */
    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();//递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    Log.e(TAG, "deleteDir: " + false);
                    return false;
                }
            }
        }
        Log.e(TAG, "deleteDir: " + true);
        // 目录此时为空，可以删除
        return dir.delete();
    }
}
